package com.demo.springboothexadecimalconversion.hextobyte;

import com.demo.springboothexadecimalconversion.constant.MsgFrame;
import com.demo.springboothexadecimalconversion.service.DecodeImpl.DeviceInfoDecodeImpl;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;

/**
 * @author Hanson
 * @date 2022/11/11 11:11
 */
@Component
public class HexToLong {

    @Resource
    private DeviceInfoDecodeImpl deviceInfoDecode;
//    @Test
//    public void test(){
//        String s = "0000018479f87dc12212000180010001000101";
//        HexDecode(s);
//    }

    /**
     * @param str 十六进制字符串
     * @return 浮点数
     */
    public void HexDecode(String str){
//        BigInteger bigInteger = new BigInteger(str, 16);
//        float value = Float.intBitsToFloat(bigInteger.byteValue());


        String timeStampHex = str.substring(5, 16);
        String SNHex = str.substring(17,24);
        String msgFrameHex = str.substring(24,26);
        String protocolVersionHex = str.substring(26,28);
        String payload = str.substring(28,str.length()-1);

        long timeStamp = Long.parseLong(timeStampHex, 16);
        Integer SN = Integer.valueOf(SNHex, 16);
        Integer msgFrame = Integer.valueOf(msgFrameHex, 16);
        Integer protocolVersion = Integer.valueOf(protocolVersionHex, 16);
        switch (MsgFrame.getByValue(msgFrame.toString())){
            case Health:
                break;
            case TurnOver:
                break;
            case OnBedStatus:
                break;
            case SleepApnea:
                break;
            case Fall:
                break;
            case OTAUpgrade:
                break;
            case OTAStatus:
                break;
            case OTAConfig:
                break;
            case DeviceInfo:
                this.deviceInfoDecode.Decode(str);
                break;
        }
        System.out.println(timeStampHex);
        System.out.println(timeStamp);
        System.out.println(SNHex);
        System.out.println(SN);
        System.out.println(msgFrameHex);
        System.out.println(msgFrame);
        System.out.println(protocolVersionHex);
        System.out.println(protocolVersion);
        System.out.println(payload);

    }


}
